/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.view;

import uk.org.aspellclark.agenedb.R;
import uk.org.aspellclark.agenedb.db.DatabaseObjectContainer;
import uk.org.aspellclark.agenedb.view.tree.FamilyTreeGraphCntrlAbs;
import uk.org.aspellclark.common.view.FullScrollView;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;

public class GedcomTreeView extends Activity {

	//private Gedcom gedcom = Gedcom.getInstance();
	private FamilyTreeGraphCntrlAbs famTreeGrphAbs;
	//private FullScrollView fsv;

	private ProgressDialog loadingDialog;
	private static final int ID_DIALOG_PROGRESS_POPULATE_LIST = 988;

	public final Handler handler = new Handler() {
		@Override
		public void handleMessage(final Message msg) {
			GedcomTreeView.this.loadingDialog.setProgress(msg.what);
		}
	};

	private static String LOG_TAG = "GedcomTreeView";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		FullScrollView fsv = new FullScrollView(this);
		MarginLayoutParams layoutParmsFsv = new MarginLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

		this.famTreeGrphAbs = new FamilyTreeGraphCntrlAbs(this);
		fsv.addView(this.famTreeGrphAbs);

		this.setContentView(fsv, layoutParmsFsv);

		this.updateFamilyTreeGraph();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		if (id == ID_DIALOG_PROGRESS_POPULATE_LIST) {
			this.loadingDialog = new ProgressDialog(this);
			this.loadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			this.loadingDialog.setTitle(this.getString(R.string.PleaseWait));
			this.loadingDialog.setMessage(this.getString(R.string.LoadingListItems));
			this.loadingDialog.setIndeterminate(true);
			this.loadingDialog.setCancelable(true);
			this.loadingDialog.setProgress(0);
			return this.loadingDialog;
		}

		return super.onCreateDialog(id);
	}

	/**
	 * stores the currently selected person.
	 * 
	 * @param prsnId
	 */
	public void updateCurrentPerson(final String prsnId) {
		try {
			if (prsnId.length() > 0) {
				// update currently selected person
				DatabaseObjectContainer.individualCntrlr.setCurrentPrsnId(prsnId);
				this.updateFamilyTreeGraph();
			}
		} catch (final Exception e) {
			Log.e(LOG_TAG, e.getMessage());
		}
	}

	/**
	 * This function populates the family tree graph.
	 */
	private void updateFamilyTreeGraph() {
		this.showDialog(ID_DIALOG_PROGRESS_POPULATE_LIST);

		try {
			this.famTreeGrphAbs.setIndividual(DatabaseObjectContainer.individualCntrlr.getCurrentPrsnId());
		} catch (Throwable t) {
			Log.e(LOG_TAG, t.getMessage());
		}
		this.removeDialog(ID_DIALOG_PROGRESS_POPULATE_LIST);
	}

}